//
//  SearchProductViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/5/3.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "SearchProductViewController.h"
#import "DanXiangArrayModel.h"
#import "DiseaseProductTableViewCell.h"
#import "DiseaseDanXiangTableViewCell.h"
#import "SearchProductPackageModel.h"
#import "ProductDetailViewController.h"
#import "DanXiangBingDetailViewController.h"
#import "AFNetworking.h"

#define kSeachUrl @"http://120.26.129.84/api/search"

@interface SearchProductViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UITableView *searchProductTableView;
@property (nonatomic, strong) UITextField *searchProductTF; // 搜索框

@property (nonatomic, strong) NSMutableArray *diseaseProductArray; // 搜索状态类数组
@property (nonatomic, strong) NSMutableArray *diseaseDanXiangArray; // 搜索状态单项数组

@property (nonatomic, assign) BOOL isSearch; // 是否在搜索
@property (nonatomic, strong) NSMutableArray *noSearchProductArray; // 在非搜索状态下的产品包数组
@property (nonatomic, strong) NSMutableArray *noSearchDanXiangArray; // 在非搜索状态下的单项数组

@property (nonatomic, strong) NSTimer *myTimer; // 检查请求是否超时
@property (nonatomic, assign) NSInteger numberOfTime; // 存储请求时间

@end

@implementation SearchProductViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    // 关闭第一响应者
    [self.searchProductTF resignFirstResponder];
    // 关闭计时器
    [self.myTimer invalidate];
    // 关闭动画
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    
    // 布局
    [self addMySubview];
    // 检查网络进行数据请求
    [self checkNetAndRequestData];
}

#pragma mark - 检查网络进行数据请求
- (void)checkNetAndRequestData
{
    if ([[[NetStatusHandle shareHandle] getNetStatus] isEqualToString:@"YES"]) {
        // 说明有网络
        // 构建非搜索状态下的数据
        [self addNoSearchData];
        // 建立计数器，计算是否超时
        self.myTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(jiShiOnSearch:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];
        self.numberOfTime = 0;
        // 开始动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
    } else {
        NSLog(@"没有网络");
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kNoNetNoRefresh ToView:self];
    }
}

#pragma mark - 计时器方法
- (void)jiShiOnSearch:(NSTimer *)timer
{
    self.numberOfTime++;
    NSLog(@"搜索页时间= %ld", self.numberOfTime);
    if (self.numberOfTime > 10) {
        NSLog(@"搜索页请求超时");
        self.numberOfTime = 0;
        [self.myTimer invalidate];
        // 关闭动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kQingQiuChaoShiNoReFresh ToView:self];
    }

}

#pragma mark - 构建非搜索状态下的数据
- (void)addNoSearchData
{
    
    self.isSearch = NO;
    // 获取最后一次的结果
    NSString *lastResultString = [[NetStatusHandle shareHandle] getLastResultString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (lastResultString.length == 0) {
        // 默认为心脏病,用户第一次用
        NSDictionary *parameterDic = [NSDictionary dictionaryWithObjectsAndKeys:@"心", @"keyword", nil];
        [manager POST:kSeachUrl parameters:parameterDic success:^(NSURLSessionDataTask *task, id responseObject) {
            if (!responseObject) {
                return;
            }
            // 将计时器置为0
            self.numberOfTime = 0;
            [self.myTimer invalidate];
            // 结束动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            
            // 构建非搜索状态下的数据
            self.noSearchProductArray = [NSMutableArray array];
            self.noSearchDanXiangArray = [NSMutableArray array];
            NSArray *productionArray = [responseObject valueForKey:@"classifications"];
            for (NSDictionary *productionDic in productionArray) {
                SearchProductPackageModel *productionModel = [[SearchProductPackageModel alloc] init];
                [productionModel setValuesForKeysWithDictionary:productionDic];
                [self.noSearchProductArray addObject:productionModel];
            }
            NSArray *danXiangArray = [responseObject valueForKey:@"diseases"];
            for (NSDictionary *danXiangDic in danXiangArray) {
                DanXiangArrayModel *danXiangModel = [[DanXiangArrayModel alloc] init];
                [danXiangModel setValuesForKeysWithDictionary:danXiangDic];
                [self.noSearchDanXiangArray addObject:danXiangModel];
            }
//            NSLog(@"非搜索状态下noSearchDanXiangArray = %@", self.noSearchDanXiangArray);
//            NSLog(@"非搜索状态下noSearchProductArray = %@", self.noSearchProductArray);
            [self.searchProductTableView reloadData];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"数据获取失败");
            // 关闭动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            // 添加提示框
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kHuoQuDataShiBaiNoRefresh ToView:self];
            // 暂停计时器
            [self.myTimer invalidate];
            self.numberOfTime = 0;
        }];

    } else {
        NSDictionary *parameterDic = [NSDictionary dictionaryWithObjectsAndKeys:lastResultString, @"keyword", nil];
        [manager POST:kSeachUrl parameters:parameterDic success:^(NSURLSessionDataTask *task, id responseObject) {
            if (!responseObject) {
                return;
            }
            // 将计时器置为0
            self.numberOfTime = 0;
            [self.myTimer invalidate];
            // 结束动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            // 构建非搜索状态下的数据
            self.noSearchProductArray = [NSMutableArray array];
            self.noSearchDanXiangArray = [NSMutableArray array];
            NSArray *productionArray = [responseObject valueForKey:@"classifications"];
            for (NSDictionary *productionDic in productionArray) {
                SearchProductPackageModel *productionModel = [[SearchProductPackageModel alloc] init];
                [productionModel setValuesForKeysWithDictionary:productionDic];
                [self.noSearchProductArray addObject:productionModel];
            }
            NSArray *danXiangArray = [responseObject valueForKey:@"diseases"];
            for (NSDictionary *danXiangDic in danXiangArray) {
                DanXiangArrayModel *danXiangModel = [[DanXiangArrayModel alloc] init];
                [danXiangModel setValuesForKeysWithDictionary:danXiangDic];
                [self.noSearchDanXiangArray addObject:danXiangModel];
            }
            NSLog(@"非搜索状态下noSearchDanXiangArray = %@", self.noSearchDanXiangArray);
            NSLog(@"非搜索状态下noSearchProductArray = %@", self.noSearchProductArray);
            [self.searchProductTableView reloadData];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"数据获取失败");
            // 关闭动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            // 添加提示框
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kHuoQuDataShiBaiNoRefresh ToView:self];
        }];
    }
    
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    
    self.navigationItem.title = @"搜索";
    // 左边
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    UIBarButtonItem *leftFirstItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    self.navigationItem.leftBarButtonItem = leftFirstItem;
   
    
    
}

#pragma mark - 返回
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}



// 布局
- (void)addMySubview
{
    self.searchProductTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.searchProductTableView.dataSource = self;
    self.searchProductTableView.delegate = self;
    self.searchProductTableView.showsVerticalScrollIndicator = NO;
    self.searchProductTableView.bounces = NO;
    [self.view addSubview:_searchProductTableView];
    
    // 去掉分割线
    self.searchProductTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    // 构建表头
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50 * kAdjustParamater)];
    headerView.backgroundColor = [UIColor whiteColor];
    headerView.userInteractionEnabled = YES;
    self.searchProductTableView.tableHeaderView = headerView;
    
    
    UIView *whiteViewOnTopHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50 * kAdjustParamater)];
    whiteViewOnTopHeader.userInteractionEnabled = YES;
    whiteViewOnTopHeader.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:whiteViewOnTopHeader];
    
    UIImageView *searchImageV = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, 17 * kAdjustParamater, 20 * kAdjustParamater, 20 * kAdjustParamater)];
    searchImageV.image = [UIImage imageNamed:@"tianjiahaoyoutubiao-tianjia"];
    [whiteViewOnTopHeader addSubview:searchImageV];
    
    self.searchProductTF = [[UITextField alloc] initWithFrame:CGRectMake(searchImageV.right + 10 * kAdjustParamater, 18 * kAdjustParamater, 300 * kAdjustParamater, 15 * kAdjustParamater)];
    self.searchProductTF.placeholder = @"请输入与基因健康相关的内容";
   // self.searchProductTF.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.searchProductTF.font = [UIFont systemFontOfSize:k15Big];
    self.searchProductTF.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    self.searchProductTF.tintColor = kCyanColor;
    self.searchProductTF.returnKeyType = UIReturnKeyDone;
    self.searchProductTF.delegate = self;
    [self.searchProductTF addTarget:self action:@selector(searchProductTFChange:) forControlEvents:UIControlEventAllEditingEvents];
    [whiteViewOnTopHeader addSubview:_searchProductTF];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.isSearch == NO) {
        // 说明是非搜索状态
        if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count > 0) {
            return 2;
        } else if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count == 0) {
            return 1;
        } else if (self.noSearchProductArray.count == 0 && self.noSearchDanXiangArray.count > 0) {
            return 1;
        }
    } else if (self.isSearch == YES) {
        // 说明是搜索状态下
        if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count > 0) {
            return 2;
        } else if (self.diseaseProductArray.count == 0 && self.diseaseDanXiangArray.count > 0) {
            return 1;
        } else if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count == 0) {
            return 1;
        }
    }
    
      return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isSearch == NO) {
        // 说明是非搜索状态
        if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count > 0) {
            if (section == 0) {
                return self.noSearchProductArray.count;
            } else if (section == 1) {
                return self.noSearchDanXiangArray.count;
            }
        } else if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count == 0) {
            return self.noSearchProductArray.count;
        } else if (self.noSearchProductArray.count == 0 && self.noSearchDanXiangArray.count > 0) {
            return self.noSearchDanXiangArray.count;
        }
    } else if (self.isSearch == YES) {
        // 说明是搜索状态
        if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count > 0) {
            if (section == 0) {
                return self.diseaseProductArray.count;
            }
            return self.diseaseDanXiangArray.count;
        } else if (self.diseaseProductArray.count == 0 && self.diseaseDanXiangArray.count > 0) {
            return self.diseaseDanXiangArray.count;
        } else if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count == 0) {
            return self.diseaseProductArray.count;
        }
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isSearch == NO) {
        // 说明是非搜索状态
        if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count > 0) {
            if (indexPath.section == 0) {
                // 产品包cell
                static NSString *identifier = @"diseaseProduct";
                DiseaseProductTableViewCell *diseaseProductCell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (diseaseProductCell == nil) {
                    diseaseProductCell = [[DiseaseProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                }
                diseaseProductCell.selectionStyle = UITableViewCellSelectionStyleNone;
                if (self.noSearchProductArray.count > indexPath.row) {
                    diseaseProductCell.model = self.noSearchProductArray[indexPath.row];
                }
                return diseaseProductCell;
            } else if (indexPath.section == 1) {
                // 疾病单项cell
                static NSString *identifier = @"diseaseDanXiang";
                DiseaseDanXiangTableViewCell *diseaseDanXiangCell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (diseaseDanXiangCell == nil) {
                    diseaseDanXiangCell = [[DiseaseDanXiangTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                }
                diseaseDanXiangCell.selectionStyle = UITableViewCellSelectionStyleNone;
                if (self.noSearchDanXiangArray.count > indexPath.row) {
                    diseaseDanXiangCell.model = self.noSearchDanXiangArray[indexPath.row];
                }
                return diseaseDanXiangCell;
            }
            
        } else if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count == 0) {
            // 产品包cell
            static NSString *identifier = @"diseaseProduct";
            DiseaseProductTableViewCell *diseaseProductCell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (diseaseProductCell == nil) {
                diseaseProductCell = [[DiseaseProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            diseaseProductCell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (self.noSearchProductArray.count > indexPath.row) {
                diseaseProductCell.model = self.noSearchProductArray[indexPath.row];
            }
            return diseaseProductCell;
        } else if (self.noSearchProductArray.count == 0 && self.noSearchDanXiangArray.count > 0) {
            // 疾病单项cell
            static NSString *identifier = @"diseaseDanXiang";
            DiseaseDanXiangTableViewCell *diseaseDanXiangCell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (diseaseDanXiangCell == nil) {
                diseaseDanXiangCell = [[DiseaseDanXiangTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            diseaseDanXiangCell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (self.noSearchDanXiangArray.count > indexPath.row) {
                diseaseDanXiangCell.model = self.noSearchDanXiangArray[indexPath.row];
            }
            return diseaseDanXiangCell;
        }
    } else if (self.isSearch == YES) {
        if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count > 0) {
            if (indexPath.section == 0) {
                // 产品包cell
                static NSString *identifier = @"diseaseProduct";
                DiseaseProductTableViewCell *diseaseProductCell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (diseaseProductCell == nil) {
                    diseaseProductCell = [[DiseaseProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                }
                diseaseProductCell.selectionStyle = UITableViewCellSelectionStyleNone;
                if (self.diseaseProductArray.count > indexPath.row) {
                    diseaseProductCell.model = self.diseaseProductArray[indexPath.row];
                }
                return diseaseProductCell;
            } else if (indexPath.section == 1) {
                // 疾病单项cell
                static NSString *identifier = @"diseaseDanXiang";
                DiseaseDanXiangTableViewCell *diseaseDanXiangCell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (diseaseDanXiangCell == nil) {
                    diseaseDanXiangCell = [[DiseaseDanXiangTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                }
                diseaseDanXiangCell.selectionStyle = UITableViewCellSelectionStyleNone;
                if (self.diseaseDanXiangArray.count > indexPath.row) {
                    diseaseDanXiangCell.model = self.diseaseDanXiangArray[indexPath.row];
                }
                return diseaseDanXiangCell;
            }
           
        } else if (self.diseaseProductArray.count == 0 && self.diseaseDanXiangArray.count > 0) {
            // 疾病单项cell
            static NSString *identifier = @"diseaseDanXiang";
            DiseaseDanXiangTableViewCell *diseaseDanXiangCell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (diseaseDanXiangCell == nil) {
                diseaseDanXiangCell = [[DiseaseDanXiangTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            diseaseDanXiangCell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (self.diseaseDanXiangArray.count > indexPath.row) {
                diseaseDanXiangCell.model = self.diseaseDanXiangArray[indexPath.row];
            }
            return diseaseDanXiangCell;
        } else if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count == 0) {
            // 产品包cell
            static NSString *identifier = @"diseaseProduct";
            DiseaseProductTableViewCell *diseaseProductCell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (diseaseProductCell == nil) {
                diseaseProductCell = [[DiseaseProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            diseaseProductCell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (self.diseaseProductArray.count > indexPath.row) {
                diseaseProductCell.model = self.diseaseProductArray[indexPath.row];
            }
            return diseaseProductCell;
            
        }

    }

        return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.isSearch == NO) {
        // 说明是非搜索状态
        if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count > 0) {
            if (section == 0) {
                // 产品包
                UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
                tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
                UIImageView *chanPinBaoImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
                chanPinBaoImageV.image = [UIImage imageNamed:@"productPackage-chanpin"];
                [tempView addSubview:chanPinBaoImageV];
                UILabel *chanPinBaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinBaoImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
                chanPinBaoLabel.text = @"检测大类";
                chanPinBaoLabel.textColor = [UIColor whiteColor];
                chanPinBaoLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
                [tempView addSubview:chanPinBaoLabel];
                return tempView;
            }
            // 单项
            UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
            tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
            UIImageView *chanPinDanXiangImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
            chanPinDanXiangImageV.image = [UIImage imageNamed:@"productDanxiang-chanpin"];
            [tempView addSubview:chanPinDanXiangImageV];
            UILabel *chanPinDanXiangLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinDanXiangImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
            chanPinDanXiangLabel.text = @"疾病单项";
            chanPinDanXiangLabel.textColor = [UIColor whiteColor];
            chanPinDanXiangLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
            [tempView addSubview:chanPinDanXiangLabel];
            return tempView;
        } else if (self.noSearchProductArray.count == 0 && self.noSearchDanXiangArray.count > 0) {
            // 单项
            UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
            tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
            UIImageView *chanPinDanXiangImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
            chanPinDanXiangImageV.image = [UIImage imageNamed:@"productDanxiang-chanpin"];
            [tempView addSubview:chanPinDanXiangImageV];
            UILabel *chanPinDanXiangLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinDanXiangImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
            chanPinDanXiangLabel.text = @"疾病单项";
            chanPinDanXiangLabel.textColor = [UIColor whiteColor];
            chanPinDanXiangLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
            [tempView addSubview:chanPinDanXiangLabel];
            return tempView;
            
        } else if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count == 0) {
            // 产品包
            UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
            tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
            UIImageView *chanPinBaoImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
            chanPinBaoImageV.image = [UIImage imageNamed:@"productPackage-chanpin"];
            [tempView addSubview:chanPinBaoImageV];
            UILabel *chanPinBaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinBaoImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
            chanPinBaoLabel.text = @"检测大类";
            chanPinBaoLabel.textColor = [UIColor whiteColor];
            chanPinBaoLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
            [tempView addSubview:chanPinBaoLabel];
            return tempView;
        }

    } else if (self.isSearch == YES) {
        if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count > 0) {
            if (section == 0) {
                // 产品包
                UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
                tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
                UIImageView *chanPinBaoImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
                chanPinBaoImageV.image = [UIImage imageNamed:@"productPackage-chanpin"];
                [tempView addSubview:chanPinBaoImageV];
                UILabel *chanPinBaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinBaoImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
                chanPinBaoLabel.text = @"检测大类";
                chanPinBaoLabel.textColor = [UIColor whiteColor];
                chanPinBaoLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
                [tempView addSubview:chanPinBaoLabel];
                return tempView;
            }
            // 单项
            UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
            tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
            UIImageView *chanPinDanXiangImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
            chanPinDanXiangImageV.image = [UIImage imageNamed:@"productDanxiang-chanpin"];
            [tempView addSubview:chanPinDanXiangImageV];
            UILabel *chanPinDanXiangLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinDanXiangImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
            chanPinDanXiangLabel.text = @"疾病单项";
            chanPinDanXiangLabel.textColor = [UIColor whiteColor];
            chanPinDanXiangLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
            [tempView addSubview:chanPinDanXiangLabel];
            return tempView;
        } else if (self.diseaseProductArray.count == 0 && self.diseaseDanXiangArray.count > 0) {
            // 单项
            UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
            tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
            UIImageView *chanPinDanXiangImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
            chanPinDanXiangImageV.image = [UIImage imageNamed:@"productDanxiang-chanpin"];
            [tempView addSubview:chanPinDanXiangImageV];
            UILabel *chanPinDanXiangLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinDanXiangImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
            chanPinDanXiangLabel.text = @"疾病单项";
            chanPinDanXiangLabel.textColor = [UIColor whiteColor];
            chanPinDanXiangLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
            [tempView addSubview:chanPinDanXiangLabel];
            return tempView;
            
        } else if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count == 0) {
            // 产品包
            UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kAdjustParamater)];
            tempView.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222/ 255.0 alpha:1];
            UIImageView *chanPinBaoImageV = [[UIImageView alloc] initWithFrame:CGRectMake(18 * kAdjustParamater, 1 * kAdjustParamater, 18 * kAdjustParamater, 18 * kAdjustParamater)];
            chanPinBaoImageV.image = [UIImage imageNamed:@"productPackage-chanpin"];
            [tempView addSubview:chanPinBaoImageV];
            UILabel *chanPinBaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanPinBaoImageV.right + 4 * kAdjustParamater, 4 * kAdjustParamater, 70 * kAdjustParamater, 12 * kAdjustParamater)];
            chanPinBaoLabel.text = @"检测大类";
            chanPinBaoLabel.textColor = [UIColor whiteColor];
            chanPinBaoLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
            [tempView addSubview:chanPinBaoLabel];
            return tempView;
        }

    }
        return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20 * kAdjustParamater;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 107 * kAdjustParamater;
}

#pragma mark - 输入框内容发生变化
- (void)searchProductTFChange:(UITextField *)textField
{
    NSLog(@"内容:%@", textField.text);
    BOOL isContainSpace = NO;
    if ([textField.text containsString:@" "]) {
        isContainSpace = YES;
    }
    if (textField.text.length >= 1 && isContainSpace == NO) {
        // 开启搜索状态
        self.isSearch = YES;
        
       
        // 数据请求
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSDictionary *parameterDic = [NSDictionary dictionaryWithObjectsAndKeys:textField.text, @"keyword", nil];
        [manager POST:kSeachUrl parameters:parameterDic success:^(NSURLSessionDataTask *task, id responseObject) {
            //     NSLog(@"responseObject = %@", responseObject);
            if (!responseObject) {
                return;
            }
            if (responseObject != nil) {
                // 保存最后一次的搜索结果
                [[NetStatusHandle shareHandle] saveLastResultString:textField.text];
            }
            
            
            // 解析数据
            self.diseaseProductArray = [NSMutableArray array];
            self.diseaseDanXiangArray = [NSMutableArray array];
            NSArray *productionArray = [responseObject valueForKey:@"classifications"];
            for (NSDictionary *productionDic in productionArray) {
                SearchProductPackageModel *productionModel = [[SearchProductPackageModel alloc] init];
                [productionModel setValuesForKeysWithDictionary:productionDic];
                [self.diseaseProductArray addObject:productionModel];
            }
            NSArray *danXiangArray = [responseObject valueForKey:@"diseases"];
            for (NSDictionary *danXiangDic in danXiangArray) {
                DanXiangArrayModel *danXiangModel = [[DanXiangArrayModel alloc] init];
                [danXiangModel setValuesForKeysWithDictionary:danXiangDic];
                [self.diseaseDanXiangArray addObject:danXiangModel];
            }
//            NSLog(@"搜索状态：self.diseaseProductArray = %@", self.diseaseProductArray);
//            NSLog(@"收缩状态：self.diseaseDanXiangArray = %@", self.diseaseDanXiangArray);
            [self.searchProductTableView reloadData];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"数据获取失败");
            // 关闭动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            // 添加提示框
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kHuoQuDataShiBaiNoRefresh ToView:self];
        }];

    } else if (textField.text.length == 0) {
        
        self.isSearch = NO;
         [self.searchProductTableView reloadData];
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"section = %ld, row = %ld", indexPath.section, indexPath.row);
    if (self.isSearch == NO) {
        // 说明是非搜索状态
        if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count > 0) {
            // 有类和单项
            if (indexPath.section == 0) {
                ProductDetailViewController *productDetailVC = [[ProductDetailViewController alloc] init];
                if (self.noSearchProductArray.count > indexPath.row) {
                   // productDetailVC.leiIDString = [self.noSearchProductArray[indexPath.row] valueForKey:@"newdetail_url"];
                    productDetailVC.leiIDString = [self.noSearchProductArray[indexPath.row] valueForKey:@"classID"];
                }
                [self.navigationController pushViewController:productDetailVC animated:YES];
            } else if (indexPath.section == 1) {
                // 这里是单项
                DanXiangBingDetailViewController *danXiangBingDetailVC = [[DanXiangBingDetailViewController alloc] init];
                if (self.noSearchDanXiangArray.count > indexPath.row) {
                    DanXiangArrayModel *model = self.noSearchDanXiangArray[indexPath.row];
                    danXiangBingDetailVC.danXiangID = [NSString stringWithFormat:@"%@", model.danXiangID];
                }
                [self.navigationController pushViewController:danXiangBingDetailVC animated:YES];
            }
        } else if (self.noSearchProductArray.count > 0 && self.noSearchDanXiangArray.count == 0) {
            // 有类无单项
            ProductDetailViewController *productDetailVC = [[ProductDetailViewController alloc] init];
            if (self.noSearchProductArray.count > indexPath.row) {
              //  productDetailVC.productDetailUrl = [self.noSearchProductArray[indexPath.row] valueForKey:@"newdetail_url"];
                productDetailVC.leiIDString = [self.noSearchProductArray[indexPath.row] valueForKey:@"classID"];
            }
            [self.navigationController pushViewController:productDetailVC animated:YES];
        } else if (self.noSearchProductArray.count == 0 && self.noSearchDanXiangArray.count > 0) {
            // 无类有单项
            DanXiangBingDetailViewController *danXiangBingDetailVC = [[DanXiangBingDetailViewController alloc] init];
            if (self.noSearchDanXiangArray.count > indexPath.row) {
                DanXiangArrayModel *model = self.noSearchDanXiangArray[indexPath.row];
                danXiangBingDetailVC.danXiangID = [NSString stringWithFormat:@"%@", model.danXiangID];
            }
            [self.navigationController pushViewController:danXiangBingDetailVC animated:YES];
        }
    } else if (self.isSearch == YES) {
        // 说明是搜索状态
        if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count > 0) {
            // 有类有单项
            if (indexPath.section == 0) {
                // 类
                ProductDetailViewController *productDetailVC = [[ProductDetailViewController alloc] init];
                if (self.diseaseProductArray.count > indexPath.row) {
                    productDetailVC.leiIDString = [self.diseaseProductArray[indexPath.row] valueForKey:@"classID"];
                }
                [self.navigationController pushViewController:productDetailVC animated:YES];
            } else if (indexPath.section == 1) {
                // 单项
                DanXiangBingDetailViewController *danXiangBingDetailVC = [[DanXiangBingDetailViewController alloc] init];
                if (self.diseaseDanXiangArray.count > indexPath.row) {
                    DanXiangArrayModel *model = self.diseaseDanXiangArray[indexPath.row];
                    danXiangBingDetailVC.danXiangID = [NSString stringWithFormat:@"%@", model.danXiangID];
                }
                [self.navigationController pushViewController:danXiangBingDetailVC animated:YES];
            }
            
        } else if (self.diseaseProductArray.count == 0 && self.diseaseDanXiangArray.count > 0) {
            // 无类有单项
            // 单项
            DanXiangBingDetailViewController *danXiangBingDetailVC = [[DanXiangBingDetailViewController alloc] init];
            if (self.diseaseDanXiangArray.count > indexPath.row) {
                DanXiangArrayModel *model = self.diseaseDanXiangArray[indexPath.row];
                danXiangBingDetailVC.danXiangID = [NSString stringWithFormat:@"%@", model.danXiangID];
            }
            [self.navigationController pushViewController:danXiangBingDetailVC animated:YES];
        } else if (self.diseaseProductArray.count > 0 && self.diseaseDanXiangArray.count == 0) {
            // 有类无单项
            ProductDetailViewController *productDetailVC = [[ProductDetailViewController alloc] init];
            if (self.diseaseProductArray.count > indexPath.row) {

                productDetailVC.leiIDString = [self.diseaseProductArray[indexPath.row] valueForKey:@"classID"];
            }
            [self.navigationController pushViewController:productDetailVC animated:YES];
        }
    }

}




- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    return YES;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
